package com.education.platform.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.education.platform.common.Result;
import com.education.platform.dto.category.CategoryCreateDTO;
import com.education.platform.entity.Category;
import com.education.platform.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类控制器
 *
 * @author education-platform
 * @since 2024-09-27
 */
@Tag(name = "分类管理", description = "分类管理相关接口")
@RestController
@RequestMapping("/api/category")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;

    @Operation(summary = "创建分类", description = "创建新分类（管理员）")
    @PostMapping
    public Result<Long> createCategory(@Validated @RequestBody CategoryCreateDTO categoryCreateDTO) {
        Long categoryId = categoryService.createCategory(categoryCreateDTO);
        return Result.success(categoryId);
    }

    @Operation(summary = "更新分类", description = "更新分类信息（管理员）")
    @PutMapping("/{id}")
    public Result<Void> updateCategory(@PathVariable Long id,
                                       @Validated @RequestBody CategoryCreateDTO categoryCreateDTO) {
        categoryService.updateCategory(id, categoryCreateDTO);
        return Result.success();
    }

    @Operation(summary = "删除分类", description = "删除分类（管理员）")
    @DeleteMapping("/{id}")
    public Result<Void> deleteCategory(@PathVariable Long id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }

    @Operation(summary = "获取分类详情", description = "根据ID获取分类详细信息")
    @GetMapping("/{id}")
    public Result<Category> getCategoryDetail(@PathVariable Long id) {
        Category category = categoryService.getCategoryById(id);
        return Result.success(category);
    }

    @Operation(summary = "获取分类树", description = "获取所有分类的树形结构")
    @GetMapping("/tree")
    public Result<List<Category>> getCategoryTree() {
        List<Category> tree = categoryService.getCategoryTree();
        return Result.success(tree);
    }

    @Operation(summary = "获取子分类列表", description = "获取指定父分类下的子分类")
    @GetMapping("/children/{parentId}")
    public Result<List<Category>> getChildCategories(@PathVariable Long parentId) {
        List<Category> children = categoryService.getChildCategories(parentId);
        return Result.success(children);
    }

    @Operation(summary = "获取启用的分类", description = "获取所有启用的分类")
    @GetMapping("/active")
    public Result<List<Category>> getActiveCategories() {
        List<Category> categories = categoryService.getActiveCategories();
        return Result.success(categories);
    }

    @Operation(summary = "更新分类状态", description = "更新分类状态（管理员）")
    @PutMapping("/{id}/status")
    public Result<Void> updateStatus(@PathVariable Long id, @RequestParam String status) {
        categoryService.updateStatus(id, status);
        return Result.success();
    }
}

